package ai.people.netmon.msgsearch.service.impl;

import ai.people.core.common.properties.elastic.ElasticSocialUserIndex;
import ai.people.netmon.framework.domain.social.user.SocialAccountSearchReqVO;
import ai.people.netmon.framework.domain.social.user.SocialAccountSearchRespListVO;
import ai.people.netmon.framework.model.response.PageResponse;
import ai.people.netmon.msgsearch.domain.entity.SocialUser;
import ai.people.netmon.msgsearch.mapper.es.SocialUserEsMapper;
import ai.people.netmon.msgsearch.service.ISocialUserEsSearchService;
import com.alibaba.cloud.commons.lang.StringUtils;
import com.google.common.collect.Lists;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author LiGuanNan
 * @date 2022/6/23 17:10
 */
@Service
public class SocialUserEsSearchServiceImpl implements ISocialUserEsSearchService {

    @Autowired
    private SocialUserEsMapper socialUserEsMapper;

    @Autowired
    @Qualifier("elasticsearchRestTemplate")
    private ElasticsearchRestTemplate elasticsearchRestTemplate;

    public SocialUser detail(String id, String source) {
        return socialUserEsMapper.findSocialUserByIdAndUserType(id, source);
    }

    /**
     * 分页查询
     *
     * @param reqParam 要求参数
     * @return {@link PageResponse}<{@link SocialAccountSearchRespListVO}>
     */
    @Override
    public PageResponse<SocialUser> queryPage(SocialAccountSearchReqVO reqParam) {
        List<SocialUser> socialUsers = Lists.newArrayList();
        long total=0, totalPage=1;

        // 创建查询对象
        NativeSearchQueryBuilder builder = new NativeSearchQueryBuilder();
        BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
        if (!StringUtils.isEmpty(reqParam.getSearchKeywords())) {
            QueryBuilder matchQuery = QueryBuilders.matchQuery("user_name", reqParam.getSearchKeywords());
            boolQuery.must(matchQuery);
        }
        builder.withQuery(boolQuery);
        builder.withPageable(PageRequest.of(reqParam.getOriginPageNo(), reqParam.getPageSize()));
        NativeSearchQuery query = builder.build();

        //指定查询索引
        IndexCoordinates indexCoordinates = IndexCoordinates.of(ElasticSocialUserIndex.getIndexName(reqParam.getInformationSources()));

        // 调用查询接口
        SearchHits<SocialUser> searchHits = elasticsearchRestTemplate.search(query, SocialUser.class, indexCoordinates);
        // 记录总数
        total = searchHits.getTotalHits();
        // 计算分页总数
        totalPage = (int) ((searchHits.getTotalHits() - 1) / reqParam.getPageSize() + 1);

        List<SearchHit<SocialUser>> hits = searchHits.getSearchHits();
        if(!CollectionUtils.isEmpty(hits)){
            socialUsers  = hits.stream().map(SearchHit::getContent).collect(Collectors.toList());
        }
        return PageResponse.builder(socialUsers, total, totalPage, reqParam.getOriginPageNo());
    }
}
